<?php
/**********************************INCLUDE*********************************** *
* **************************************************************************** */
include_once '../account/Account.php';

/**
 * This class stores all information about a users' session.
 *
 * @author nicholas malacarne <nicholas.malacarne@gmail.com>
 */
class App {
    
    // session ID -nm
    const SESSION_NAME = "APP";
    
    // current account -nm
    private $account;
    
    // constructor -nm
    private function __construct(){
        
        // set current account to null -nm
        $this->account = NULL;
        
    } // end constructor -nm
    
    // destructor -nm
    public function __destruct() {
        
        // serialize object to session -nm
        $_SESSION[ self::SESSION_NAME ] = serialize( $this );
        
        // close user session -nm
        session_write_close();
        
    } // end destructor -nm
    
    // init method -nm
    public static function init(){
        
        //include_once '../account/Account.php';
        
         // start user session -nm
         session_start();
         
         if ( isset( $_SESSION[ self::SESSION_NAME ] ) === TRUE ){
             
             // unserialize and return the existing object -nm
             return unserialize( $_SESSION[ self::SESSION_NAME ] );
             
         }else{
             
             return new self();
         }
    } // end method -nm
    
    // used to set current account -nm
    public function setAccount( Account $aAccount = NULL ){
        
        $this->account = $aAccount;
        
    } // end method -nm
    
    // used to determine if the user is logged into an account -nm
    public function isLoggedIn(){
        
        return isset( $this->account );
        
    } // end method -nm
    
    // returns an associative array which app data -nm
    public function getInfo(){
        
        // TODO: return any other app data needed -nm
        
        // account info -nm
        if ( isset( $this->account ) ){ return $this->account->getInfo(); }
        
        // return null if no other return -nm
        return null;
        
    } // end method -nm
    
    // determines if all required parameters are found -nm
    public static function paramOK( $required = array(), $found = array() ){
        
        // check each required parameter key -nm
        foreach ( $required as $key ){
            
            // if key is not found -nm
            if ( ! array_key_exists( $key, $found ) ){
                
                // parameters are not ok -nm
                return FALSE;
            }
        }
        
        // all required params are found -nm
        return TRUE;
        
    } // end method -nm
    
} // end class -nm
